
;
(function($, $$) {
    $.fn.tip = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.Tip);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.Tip = function(self, options) {
        if (!(this instanceof $$.Tip)) {
            return new $$.Tip(self, options);
        }
        var defaults = {

            tipClass : "uiCornerAll",

            borderColor : "#315389",

            backgroundColor : "#315389",

            backgroundClass : $$.defaultGradientBG,

            target : null,

            direction : "top",

            arrowPositionValue : "40%",

            tipWidth : 0,

            tipHeight : 0,

            showAnimate : "slideDown",

            hideAnimate : "slideUp",

            beforeShow : null,

            show : null,

            beforeHide : null,

            hide : null,


            targetObj : null,

            tipStyle : null,

            arrowOffset : 10,

            tipHTML : '<div></div><div></div>'
        };
        var opts = $.extend(defaults, options);
        $$.Tip.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.Tip, $$.Widget, {

        init : function() {
            var tipsObj = this;
            tipsObj.createWidget();
            initTip(tipsObj.jqObj, tipsObj.opts);
            return tipsObj;
        },

        content : function(content, resetPosition) {
            var tipObj = this, self = tipObj.jqObj, opts = tipObj.opts;
            var contentObj = self.children(".uiTipContent");
            if (content === undefined) {
                return contentObj;
            }
            contentObj.html(content);
            if (resetPosition === true) {
                setPosition(self, opts);
            }
            return tipObj;
        },

        arrowPosition : function(value) {
            var tipObj = this, self = tipObj.jqObj, opts = tipObj.opts;
            var arrowList = self.children(".uiTipArrowStyle1, .uiTipArrowStyle2");
            if (value === undefined) {
                return opts.positionValue;
            }
            if (opts.tipStyle === 1) {
                arrowList.css("left", value);
            }
            else {
                arrowList.css("top", value);
            }
            opts.positionValue = value;
            return tipObj;
        },

        beforeDestroy : function() {
            var tipObj = this, self = tipObj.jqObj, opts = tipObj.opts;
            opts.targetObj.unbind("." + opts.widgetKey);
            return tipObj;
        }
    });

    function initTip(self, opts) {
        opts.targetObj = $(opts.target);
        var tipObj = $(opts.tipHTML);
        if (opts.direction === "top" || opts.direction === "bottom") {
            tipObj.addClass("uiTipArrowStyle1");
            opts.tipStyle = 1;
        }
        else {
            opts.tipStyle = 2;
            tipObj.addClass("uiTipArrowStyle2");
        }
        var contentObj = self.children();
        if (contentObj.length === 0) {
            self.wrapInner('<div class="uiTipContent" />');
        }
        else {
            contentObj.addClass("uiTipContent");
        }
        self.addClass("uiTip uiWidget " + opts.tipClass).prepend(tipObj);
        var cssShow = { position: "absolute", visibility: "hidden", display: "block" };
        $.swap(self[0], cssShow, function() {
            setPosition(self, opts);
        });
        initEvent(self, opts);
        self.hide();
        return tipObj;
    }


    function initEvent(self, opts) {
        opts.targetObj.bind("mouseenter." + opts.widgetKey, function(e) {
            var target = $(this);
            if ($.isFunction(opts.beforeShow)) {
                if (opts.beforeShow.call(self[0], target, e) === false) {
                    return;
                }
            }
            self.stop(true, true)[opts.showAnimate](opts.animateTime, function() {
                if ($.isFunction(opts.show)) {
                    opts.show.call(self[0], target);
                }
            });
        })
            .bind("mouseleave." + opts.widgetKey, function(e) {
                var target = $(this);
                if ($.isFunction(opts.beforeHide)) {
                    if (opts.beforeHide.call(self[0], target, e) === false) {
                        return;
                    }
                }
                self.stop(true, true)[opts.hideAnimate](opts.animateTime, function() {
                    if ($.isFunction(opts.hide)) {
                        opts.hide.call(self[0], target);
                    }
                });
            });
    }


    function setPosition(self, opts) {
        var leftValue = opts.targetObj.offset().left;
        var topValue = opts.targetObj.offset().top;
        var targetWidth = opts.targetObj.outerWidth();
        var targetHeight = opts.targetObj.outerHeight();
        var arrowList = self.children(".uiTipArrowStyle1, .uiTipArrowStyle2");
        var arrow1 = arrowList.eq(0);
        var arrow2 = arrowList.eq(1);
        var tipWidth = 0, tipHeight = 0;
        var setting = {
            top : {
                border : "border-top-color",
                borderNone : "border-bottom"
            },
            bottom : {
                border : "border-bottom-color",
                borderNone : "border-top"
            },
            left : {
                border : "border-left-color",
                borderNone : "border-right"
            },
            right : {
                border : "border-right-color",
                borderNone : "border-left"
            }
        };
        if (opts.tipWidth !== 0) {
            self.width(opts.tipWidth);
            tipWidth = opts.tipWidth;
        }
        else {
            tipWidth = self.width();
        }
        if (opts.tipHeight !== 0) {
            self.height(opts.tipHeight);
            tipHeight = opts.tipHeight;
        }
        else {
            tipHeight = self.height();
        }

        self.css("borderColor", opts.borderColor);
        if (opts.backgroundClass === null) {
            self.css("backgroundColor", opts.backgroundColor);
        }
        else {
            self.addClass(opts.backgroundClass);
        }

        arrow1.css(setting[opts.direction].border, opts.borderColor);
        arrow2.css(setting[opts.direction].border, opts.backgroundColor);
        arrowList.css(setting[opts.direction].borderNone, "none");
        var arrow1TopValue, arrow2TopValue, arrow1LeftValue, arrow2LeftValue;
        arrow1TopValue = arrow2TopValue = arrow1LeftValue = arrow2LeftValue = opts.arrowPositionValue;
        if (opts.tipStyle === 1) {
            leftValue += (targetWidth / 2 - tipWidth / 2);
            arrow1TopValue = -opts.arrowOffset;
            arrow2TopValue = arrow1TopValue + 1;
            if (opts.direction === "bottom") {
                topValue += (targetHeight + opts.arrowOffset);
            }
            else
            if (opts.direction === "top") {
                topValue -= (tipHeight + opts.arrowOffset);
                arrow1TopValue = tipHeight;
                arrow2TopValue = arrow1TopValue - 1;
            }
        }
        else {
            arrow1LeftValue = tipWidth;
            arrow2LeftValue = arrow1LeftValue - 1;
            if (opts.direction === "left") {
                leftValue -= (tipWidth + opts.arrowOffset);
            }
            else
            if (opts.direction === "right") {
                leftValue += (targetWidth + opts.arrowOffset);
                arrow1LeftValue = -opts.arrowOffset;
                arrow2LeftValue = arrow1LeftValue + 1;
            }
        }
        self.css({"left" : leftValue, "top" : topValue});
        arrow1.css({"top" : arrow1TopValue, "left" : arrow1LeftValue});
        arrow2.css({"top" : arrow2TopValue, "left" : arrow2LeftValue});
        return self;
    }
})(jQuery, BASE);